import BaseView from "../../../cfw/mvc/BaseView";
import BornController from "../BornController";
import ModuleManager, { ModuleID } from "../../../cfw/module/ModuleManager";
import { EventName, MAX_STATE_NUM, PlayerStateID, ActionType } from "../../../public/Config";
import GlobalEvent from "../../../cfw/event/GlobalEvent";
import DininghallManager from "../../dininghall/model/DininghallManager";
import BathRoomManager from "../../bathroom/model/BathRoomManager";
import BadRoomManager from "../../badroom/model/BadRoomManager";
import PlayerView from "../../player/PlayerView";
import Player from "../../player/Player";
import LobbyButtonView from "../../lobby/LobbyButtonView";
import SDKManager from "../../../sdk/SDKManager";
import { SDKEventName } from "../../../sdk/SDKConfig";


const { ccclass, property } = cc._decorator;

@ccclass
export default class BornView extends BaseView {


    @property(cc.Node)
    box: cc.Node = null;

    @property(cc.Node)
    player: cc.Node = null;

    @property(cc.Node)
    content: cc.Node = null;

    @property(cc.Node)
    buttonLayer: cc.Node = null;


    @property([LobbyButtonView])
    buttons: LobbyButtonView[] = []

    private step: number = 0;

    private len: number = 5;
    protected childComp: cc.Component;
    protected controller: BornController;
    protected model: Player;
    start() {
        this.controller = BornController.instance()
        GlobalEvent.instance().on(EventName.UPDATE_SLEEP_STEP, this.updateStep, this)
        this.buttons[0].node.opacity = 0;
        this.buttons[1].node.opacity = 0;
        this.buttons[2].node.opacity = 0;
        this.startStep(0)
    }
    onDestroy() {
        GlobalEvent.instance().off(EventName.UPDATE_SLEEP_STEP, this.updateStep, this)
        ModuleManager.getLoader(ModuleID.BORN).release()
    }

    startStep(n: number) {
        cc.log(' startStep n ', n)
        // this.steps[n].start()
        this.step = n;
        switch (n) {
            case 0:
                this.box.active = true;
                this.player.active = false;
                this.buttonLayer.active = false;
                break;
            case 1:
                this.box.active = false;
                this.player.active = true;
                cc.tween(this).to(1, {}).call(() => {
                    this.buttons[0].node.opacity = 255;

                    this.buttonLayer.active = true;

                    GlobalEvent.instance().emit(EventName.BROKEN_SHELL)

                }).start();

                break;
            case 2:
                let m = DininghallManager.instance();
                // cc.log(m)
                this.controller.addToParent('prefabs/BornStepView0', 'BornStepView0', m, ModuleManager.getLoader(), this.content, (comp) => {
                    this.childComp = comp
                    cc.log(' comp ', comp)
                })
                break;
            case 3:
                this.controller.addToParent('prefabs/BornStepView1', 'BornStepView1', BadRoomManager.instance(), ModuleManager.getLoader(), this.content, (comp) => {
                    this.childComp = comp
                })
                break;
            case 4:
                this.controller.addToParent('prefabs/BornStepView2', 'BornStepView2', BathRoomManager.instance(), ModuleManager.getLoader(), this.content, (comp) => {
                    this.childComp = comp
                })
                break;

        }
        SDKManager.getChannel().sendEvent(SDKEventName.born,this.step)

    }

    updateStep(step: number) {

        // this.steps[this.step].node.active = false;
        // this.steps[step].node.active = false;
        cc.log(' step == ', step)
        if (step >= this.len - 1) {
            if (this.childComp) {
                this.childComp.node.destroy()
                this.childComp = null;
            }

            this.model.setStateTime(PlayerStateID.WASH, MAX_STATE_NUM)
            // this.buttons[2].setPercent(1)
            this.buttons[2].setArrowVisible(false)
            cc.tween(this).to(1, {}).call(() => {
                this.model.updateLevel()
                GlobalEvent.instance().emit(EventName.CHANGE_PLAYER_ACTION, ActionType.SHOW_LEG)
                cc.tween(this).to(2, {}).call(() => {
                    this.controller.back()
                }).start()
            }).start()


        } else {
            switch (step) {
                case 2:
                    if (this.childComp) {
                        this.childComp.node.destroy()
                        this.childComp = null;
                    }
                    this.model.setStateTime(PlayerStateID.EATING, MAX_STATE_NUM)
                    // this.buttons[1].node.active = true;
                    this.buttons[0].setArrowVisible(false)
                    this.buttons[1].node.opacity = 255;

                    // this.buttons[0].setPercent(1)
                    break;
                case 3:
                    if (this.childComp) {
                        this.childComp.node.destroy()
                        this.childComp = null;
                    }
                    this.model.setStateTime(PlayerStateID.SLEEP, MAX_STATE_NUM)
                    // this.buttons[2].node.active = true;
                    this.buttons[1].setArrowVisible(false)
                    this.buttons[2].node.opacity = 255;
                    // this.buttons[1].setPercent(1)
                    break;
            }

        }
    }
    // update (dt) {}

    onBoxClick() {
        this.startStep(1)
    }

    onEeatingClick() {
        this.startStep(2)
    }

    onSleepClick() {
        this.startStep(3)
    }

    onWashClick() {
        this.startStep(4)
    }
}
